<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产线编号" prop="GropSc">
        <el-input
          v-model="queryParams.GropSc"
          placeholder="请输入产线编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品型号" prop="BARCODE">
        <el-input
          v-model="queryParams.BARCODE"
          placeholder="请输入产品型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位编号" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入库位编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="托盘编号" prop="CTL">
        <el-input
          v-model="queryParams.CTL"
          placeholder="请输入托盘编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="GOODSID">
        <el-input
          v-model="queryParams.GOODSID"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="GOODSNAME">
        <el-input
          v-model="queryParams.GOODSNAME"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料规格" prop="specification">
        <el-input
          v-model="queryParams.SPECIFICATION"
          placeholder="请输入物料规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.rk_sts"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
      </el-form-item>
      <div class="float-right-container">
        <div class="chart-container">
          <div class="bar-container">
            <div class="bar" style="background-color: #faa5ac; height: 20px;"></div>
            <div class="label">未执行</div>
          </div>
          <div class="bar-container">
            <div class="bar" style="background-color: #ead6a3; height: 20px;"></div>
            <div class="label">输送机已写入</div>
          </div>
          <div class="bar-container">
            <div class="bar" style="background-color: #a9bfed; height: 20px;"></div>
            <div class="label">堆垛机已写入</div>
          </div>
          <div class="bar-container">
            <div class="bar" style="background-color: #a7dc83; height: 20px;"></div>
            <div class="label">堆垛机放货完成</div>
          </div>
        </div>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:ONGOODSSHELF:edit']"
        >修改状态</el-button>

      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :row-style="rowStyle" :data="ONGOODSSHELFList" @selection-change="handleSelectionChange">
      <el-table-column type="selection"  width="55" align="center" />
      <el-table-column
        fixed
        type="index"
        label="行号"
        width="60"
        align="center"
      />
      <el-table-column label="产线编号" align="center" prop="gropSc" />
      <el-table-column label="产品型号" align="center" prop="barcode" />
      <el-table-column label="入库单号" sortable align="center" prop="lotId" />
      <el-table-column label="托盘" sortable align="center" prop="ctl" />
      <el-table-column label="库位" sortable align="center" prop="locationId" />
      <el-table-column label="物料编号" sortable align="center" prop="goodsid" />
      <el-table-column label="备注" sortable align="center" prop="goodsname" />

      <el-table-column label="数量" sortable align="center" prop="shelfNum" />
<!--      <el-table-column label="stockNum" align="center" prop="stockNum" />-->
      <el-table-column label="入库时间" align="center" prop="ONDATE" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ONDATE, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="" align="center" prop="ONSHELFUSERID" />-->
<!--      <el-table-column label="仓库" align="center" prop="storageId" />-->
<!--      <el-table-column label="存储方式" align="center" prop="storageMode" />-->
<!--      <el-table-column label="" align="center" prop="productionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->

<!--      <el-table-column label="任务所在" align="center" prop="onshelfuserid" />-->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rk_sts" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="任务编号" sortable  align="center" prop="plcID" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="单位" align="center" prop="UNIT" />-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="" prop="lotId">
          <el-input v-model="form.lotId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="GOODSID">
          <el-input v-model="form.GOODSID" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="providerId">
          <el-input v-model="form.providerId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="locationId">
          <el-input v-model="form.locationId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="ONDATE">
          <el-date-picker clearable
            v-model="form.ONDATE"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="" prop="ONSHELFUSERID">
          <el-input v-model="form.ONSHELFUSERID" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="storageId">
          <el-input v-model="form.storageId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="storageAreaId">
          <el-input v-model="form.storageAreaId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="upgoodsId">
          <el-input v-model="form.upgoodsId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="HASVOLUME">
          <el-input v-model="form.HASVOLUME" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="goodsTypeid">
          <el-input v-model="form.goodsTypeid" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="storageMode">
          <el-input v-model="form.storageMode" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="productionDate">
          <el-date-picker clearable
            v-model="form.productionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="" prop="CTL">
          <el-input v-model="form.CTL" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="BARCODE">
          <el-input v-model="form.BARCODE" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="customerId">
          <el-input v-model="form.customerId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="GOODSNAME">
          <el-input v-model="form.GOODSNAME" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="PlcID">
          <el-input v-model="form.PlcID" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="HIGH">
          <el-input v-model="form.HIGH" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="ProDucLotId">
          <el-input v-model="form.ProDucLotId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="UNIT">
          <el-input v-model="form.UNIT" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="baozhiqi">
          <el-input v-model="form.baozhiqi" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="Whseloc">
          <el-input v-model="form.Whseloc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="instand">
          <el-input v-model="form.instand" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="wgh">
          <el-input v-model="form.wgh" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="decid">
          <el-input v-model="form.decid" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="goodsMeasureId">
          <el-input v-model="form.goodsMeasureId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="damageLocation">
          <el-input v-model="form.damageLocation" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="warehousingId">
          <el-input v-model="form.warehousingId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="lerver">
          <el-input v-model="form.lerver" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="REMARK">
          <el-input v-model="form.REMARK" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="endInstand">
          <el-input v-model="form.endInstand" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="VERSION">
          <el-input v-model="form.VERSION" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="PARTDESC">
          <el-input v-model="form.PARTDESC" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="BarCodeTpe">
          <el-input v-model="form.BarCodeTpe" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="BarCodef">
          <el-input v-model="form.BarCodef" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="GropSc">
          <el-input v-model="form.GropSc" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="lx">
          <el-input v-model="form.lx" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="netWeigh">
          <el-input v-model="form.netWeigh" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="SPECIFICATION">
          <el-input v-model="form.SPECIFICATION" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="gltrp">
          <el-input v-model="form.gltrp" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="VKBUR">
          <el-input v-model="form.VKBUR" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="KUNAG">
          <el-input v-model="form.KUNAG" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="ZVKBUR">
          <el-input v-model="form.ZVKBUR" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="ZKUNAG">
          <el-input v-model="form.ZKUNAG" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="ZSOURCE">
          <el-input v-model="form.ZSOURCE" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="QTBY1">
          <el-input v-model="form.QTBY1" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="QTBY2">
          <el-input v-model="form.QTBY2" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="QTBY3">
          <el-input v-model="form.QTBY3" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="QTBY4">
          <el-input v-model="form.QTBY4" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="QTBY5">
          <el-input v-model="form.QTBY5" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="QTBY6">
          <el-input v-model="form.QTBY6" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="修改订单状态"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :model="form">
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.rk_sts"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.float-right-container {
  float: right; /* 容器向右浮动 */
  clear: both; /* 清除浮动，避免影响后续元素 */
}
.chart-container {
  display: flex;
  align-items: center; /* 使所有条形图和标签垂直居中 */
  gap: 10px; /* 条形图之间的间隔 */
}

.bar-container {
  display: flex;
  flex-direction: column;
  align-items: center; /* 使条形图和标签水平居中 */
}

.bar {
  width: 100px; /* 假设条形图的宽度为50px */
  margin-bottom: 5px; /* 条形图和标签之间的间隔 */
}

.label {
  font-size: 14px; /* 标签的字体大小 */
  color: #333; /* 标签的文字颜色 */
}
</style>
<script>
import { listONGOODSSHELF,updateSts ,updateLocations,getONGOODSSHELF, delONGOODSSHELF, addONGOODSSHELF, updateONGOODSSHELF } from "@/api/system/ONGOODSSHELF";

export default {
  name: "ONGOODSSHELF",
  dicts:['rk_sts'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      ONGOODSSHELFList: [],
      dialogVisible:false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lotId: null,
        GOODSID: null,
        providerId: null,
        locationId: null,
        stoNum: null,
        accNum: null,
        shelfNum: null,
        stockNum: null,
        ONDATE: null,
        ONSHELFUSERID: null,
        storageId: null,
        storageAreaId: null,
        upgoodsId: null,
        HASVOLUME: null,
        goodsTypeid: null,
        storageMode: null,
        productionDate: null,
        GOODSVOLUME: null,
        CTL: null,
        BARCODE: null,
        customerId: null,
        GOODSNAME: null,
        PlcID: null,
        HIGH: null,
        status: null,
        ProDucLotId: null,
        UNIT: null,
        baozhiqi: null,
        Whseloc: null,
        instand: null,
        wgh: null,
        decid: null,
        TASKTYPE: null,
        goodsMeasureId: null,
        packingNum: null,
        damageNum: null,
        damageLocation: null,
        warehousingId: null,
        lerver: null,
        REMARK: null,
        endInstand: null,
        VERSION: null,
        PARTDESC: null,
        endtype: null,
        BarCodeTpe: null,
        BarCodef: null,
        GropSc: null,
        lx: null,
        netWeigh: null,
        SPECIFICATION: null,
        gltrp: null,
        VKBUR: null,
        KUNAG: null,
        ZVKBUR: null,
        ZKUNAG: null,
        ZSOURCE: null,
        QTBY1: null,
        QTBY2: null,
        QTBY3: null,
        QTBY4: null,
        QTBY5: null,
        QTBY6: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      ctl:[],
      locationId:[],
      refreshInterval: null
    };
  },
  created() {
    this.getList();
    this.startAutoRefresh();
  },
  beforeDestroy() {
    this.stopAutoRefresh();
  },
  methods: {
    startAutoRefresh() {
      this.refreshInterval = setInterval(() => {
        this.getList();
      }, 60000); // 60 seconds
    },
    stopAutoRefresh() {
      if (this.refreshInterval) {
        clearInterval(this.refreshInterval);
      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listONGOODSSHELF(this.queryParams).then(response => {
        this.ONGOODSSHELFList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单刷新
    reset() {
      this.form = {
        lotId: null,
        GOODSID: null,
        providerId: null,
        locationId: null,
        stoNum: null,
        accNum: null,
        shelfNum: null,
        stockNum: null,
        ONDATE: null,
        ONSHELFUSERID: null,
        storageId: null,
        storageAreaId: null,
        upgoodsId: null,
        HASVOLUME: null,
        goodsTypeid: null,
        storageMode: null,
        productionDate: null,
        GOODSVOLUME: null,
        CTL: null,
        BARCODE: null,
        customerId: null,
        GOODSNAME: null,
        PlcID: null,
        HIGH: null,
        status: null,
        ProDucLotId: null,
        UNIT: null,
        baozhiqi: null,
        Whseloc: null,
        instand: null,
        wgh: null,
        decid: null,
        TASKTYPE: null,
        goodsMeasureId: null,
        packingNum: null,
        damageNum: null,
        damageLocation: null,
        warehousingId: null,
        lerver: null,
        REMARK: null,
        endInstand: null,
        VERSION: null,
        PARTDESC: null,
        endtype: null,
        BarCodeTpe: null,
        BarCodef: null,
        GropSc: null,
        lx: null,
        netWeigh: null,
        SPECIFICATION: null,
        gltrp: null,
        VKBUR: null,
        KUNAG: null,
        ZVKBUR: null,
        ZKUNAG: null,
        ZSOURCE: null,
        QTBY1: null,
        QTBY2: null,
        QTBY3: null,
        QTBY4: null,
        QTBY5: null,
        QTBY6: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 刷新按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lotId)
      this.goodsId = selection.map(item => item.goodsId)
      this.locationIds = selection.map(item => item.locationId)
      this.ctl = selection.map(item => item.ctl)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
        this.dialogVisible = true; // 显示对话框
    },
    rowStyle({row, rowIndex}) {
      if (row.status === "0") {
        return {backgroundColor: '#faa5ac'}; // 返回被点击行的样式
      }else if (row.status === '1') {
        return {backgroundColor: '#ead6a3'};
      }else if (row.status === '2') {
        return {backgroundColor: '#a9bfed'};
      }else if (row.status === '10') {
        return {backgroundColor: '#a7dc83'};
      }
      return {}; // 返回其他行的默认样式
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.lotId != null) {
            updateONGOODSSHELF(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addONGOODSSHELF(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (row.status !=='0'){
        this.$modal.msgWarning("只能删除未执行的任务");
        return
      }
      const lotIds = row.lotId || this.ids;
      const locationIds = row.locationId || this.locationIds;
      this.$modal.confirm('确认删除此任务？').then(function() {
        updateLocations(locationIds);
        return delONGOODSSHELF(lotIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ONGOODSSHELF/export', {
        ...this.queryParams
      }, `ONGOODSSHELF_${new Date().getTime()}.xlsx`)
    },

    //修改状态
    handleConfirm(){
      this.$modal.confirm('是否修改此库位的订单状态吗？').then(() => {
      updateSts({locationId:this.locationId,status: this.form.status}).then(response=>{
        this.$modal.msgSuccess("修改成功");
        this.getList();
        this.dialogVisible = false; // 显示对话框
      })
      }).catch(() => {});
    }
  }
};
</script>
